/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fase2.pt.iul.sid.view;

import fase2.pt.iul.sid.model.FotografiaModel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;
import sun.java2d.pipe.DrawImage;

/**
 *
 * @author Ricardo Marques
 */
public class Fotografia extends javax.swing.JFrame {
    
    private JFileChooser fileUpload;
    private int SelectedIdImage = 0;
    /**
     * Creates new form Fotografia
     */
    public Fotografia() {
        initComponents();
        
        this.setResizable(false);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        jButton1.setEnabled(false);
        jButton2.setEnabled(false);
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooser1 = new javax.swing.JFileChooser();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        
       
        
        jButton1.setText("Upload");

        jButton2.setText("Delete");

        jLabel1.setText("A visualizar fotografias do");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(91, 91, 91)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(94, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(414, 414, 414)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void apagar(){
        
    }
    
    public void adicionar(){
        
    }
    
    public void mostrar(List<FotografiaModel> fotos, final String email_user){
        
        JFrame frame = new JFrame();
        final JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0,1));

        for (final FotografiaModel foto : fotos) {
                        
            BufferedImage bufferrImage = null;
            try{
                bufferrImage = ImageIO.read(new File(foto.getLocalizacao()));
            }catch(Exception e){
                
            }
            
            if(bufferrImage!=null){
            
                Double w = getDoubleImage(bufferrImage.getWidth());
                Double h = getDoubleImage(bufferrImage.getHeight());

                //Adicionar imagem a uma label
                Image image = scale(bufferrImage, 1, 150, 150, w, h);
                JLabel label = new JLabel(new ImageIcon(image));
                panel.add(label);

                final JPanel item = new JPanel();
                item.setLayout(new GridLayout(0,3));
                item.add(label);
                item.add(new JLabel("<html><body>Fotografia adicionada por:<br>"+foto.getNomeUtilizador()+"<br>"+foto.getEmailUtilizador()+"</body></html>"));

                //Label que guarda o id da fotografia
                JLabel idFoto = new JLabel(""+foto.getId());
                idFoto.setName("idFotografia");
                idFoto.setVisible(false);
                item.add(idFoto);

                Border border = BorderFactory.createMatteBorder(5, 5, 0, 5, Color.gray);
                item.setBorder(border);
                panel.add(item);

                item.addMouseListener(new MouseListener() {

                    @Override
                    public void mouseClicked(MouseEvent e) {

                        Component[] comps = item.getComponents();

                        for (Component comp : panel.getComponents()) {
                            comp.setBackground(UIManager.getColor("Panel.background"));
                        }

                        for (Component comp : comps) {
                            if (comp instanceof Container && "idFotografia".equals(comp.getName())){

                                JLabel labelIdFotografia = (JLabel)comp;
                                if(email_user.equals(foto.getEmailUtilizador())){
                                    jButton2.setEnabled(true);
                                    SelectedIdImage = Integer.parseInt(labelIdFotografia.getText());
                                }else{
                                    jButton2.setEnabled(false);
                                    SelectedIdImage=0;
                                }
                                item.setBackground(Color.LIGHT_GRAY);
                                
                            }
                        }
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {

                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {

                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {

                    }

                    @Override
                    public void mouseExited(MouseEvent e) {

                    }

                });
 
            }
        }
        
        JScrollPane scrollPane = new JScrollPane(panel);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setBounds(17, 34, 600, 400);
        this.add(scrollPane);

    }
    
    private boolean pode_manipular(){
        
        boolean ret = false;
        
        return ret;
        
    }
    
    private static BufferedImage scale(BufferedImage sbi, int imageType, int dWidth, int dHeight, double fWidth, double fHeight) {
        BufferedImage dbi = null;
        if(sbi != null) {
            dbi = new BufferedImage(dWidth, dHeight, imageType);
            Graphics2D g = dbi.createGraphics();
            AffineTransform at = AffineTransform.getScaleInstance(fWidth, fHeight);
            g.drawRenderedImage(sbi, at);
        }
        return dbi;
    }
    
    private Double getDoubleImage(int size){
        
        Double ret = 0.0;
        
        if(size<=200){
            ret = 1.0;
        }else{
            ret = 0.6;
        }
        
        return ret;
        
    }

    public int getSelectedIdImage() {
        return SelectedIdImage;
    }
    
    public void addBtnActionLister(ActionListener listner){
        
        this.jButton1.addActionListener(listner);
        this.jButton2.addActionListener(listner);
        
    }

    public JButton getjButton1() {
        return jButton1;
    }

    public JButton getjButton2() {
        return jButton2;
    }
    
    public void msgbox(String s){
        JOptionPane.showMessageDialog(this,s);
    }

    public JLabel getjLabel1() {
        return jLabel1;
    }

    public JFileChooser getFileUpload() {
        return fileUpload;
    }
    
    public void setFileUpload(JFileChooser fileUpload) {
        this.fileUpload=fileUpload;
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}
